package com.xiaoyu.lanling.feature.profile.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseBottomSheetDialogFragment
import com.xiaoyu.lanling.media.selector.MediaSelector
import com.xiaoyu.lanling.media.selector.MediaSelectorFromType
import kotlinx.android.synthetic.main.user_profile_avatar_guide_fragment.*

class AvatarDemoBottomSheetDialog : BaseBottomSheetDialogFragment() {

    companion object {

        private val TAG: String = AvatarDemoBottomSheetDialog::class.java.simpleName

        fun show(fragmentManager: FragmentManager) {
            val fragment: BaseBottomSheetDialogFragment = (fragmentManager.findFragmentByTag(TAG) as? BaseBottomSheetDialogFragment)
                    ?: AvatarDemoBottomSheetDialog()
            fragment.show(fragmentManager, TAG)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(BottomSheetDialogFragment.STYLE_NO_TITLE, R.style.AvatarDemoBottomSheetDialog)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.user_profile_avatar_guide_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initBind()
    }

    private fun initBind() {
        button.setOnClickDebounceListener {
            MediaSelector.instance.selectorForImageWithCrop(true, MediaSelectorFromType.USER_INFO_EDIT)
            dismiss()
        }
    }
}